/**
 * 
 */
package com.google.code.jkippt.http.paging;

import java.util.Iterator;

import com.google.code.jkippt.parser.PageParser;
import com.google.code.jkippt.parser.ParserException;

/**
 * Iterator over page items.
 * @author Federico De Faveri defaveri@gmail.com
 *
 */
public class PageItemIterator<T> implements Iterator<T> {

	protected PageParser<T> parser;
	
	/**
	 * @param parser
	 */
	protected PageItemIterator(PageParser<T> parser) {
		this.parser = parser;
	}

	/**
	 * {@inheritDoc}
	 */
	public boolean hasNext() {
		try {
			return parser.hasMoreObjects();
		} catch (ParserException e) {
			throw new RuntimeException("An error occurred checking the next item availability", e);
		}
	}
	
	/**
	 * {@inheritDoc}
	 */
	public T next() {
		if (!hasNext()) throw new IllegalStateException("There are no more elements");
		try {
			return parser.nextObject();
		} catch (ParserException e) {
			throw new RuntimeException("An error occurred getting the next element", e);
		}
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void remove() {
		throw new UnsupportedOperationException();
	}

}
